<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme: dark infographic macarons roma shine vintage
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ title, legend, xAxisData, yAxisLabel, expectedLabel, expectedData, actualLabel, actualData } = {}) {
      const expectedOption = [{
        name: expectedLabel,
        itemStyle: {
          normal: {
            color: '#FF005A',
            lineStyle: {
              color: '#FF005A',
              width: 1
            }
          }
        },
        smooth: true,
        type: 'line',
        data: expectedData,
        animationDuration: 2800,
        animationEasing: 'cubicInOut',
        markPoint: {
          axisLabel: {
            formatter: yAxisLabel || '{value}'
          },
          data: [
            { type: 'max', name: expectedLabel }
          ]
        },
        markLine: {
          axisLabel: {
            formatter: yAxisLabel || '{value}'
          },
          data: [
            { type: 'average', name: '平均' }
          ]
        }
      }]

      let seriesOption = expectedOption

      if (actualLabel !== undefined) {
        const actualOption = [{
          name: actualLabel,
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 1
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          smooth: true,
          type: 'line',
          data: actualData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut',
          markPoint: {
            axisLabel: {
              formatter: yAxisLabel || '{value}'
            },
            data: [
              { type: 'min', name: actualLabel }
            ]
          },
          markLine: {
            axisLabel: {
              formatter: yAxisLabel || '{value}'
            },
            data: [
              { type: 'average', name: '平均' }
            ]
          }
        }]
        seriesOption = seriesOption.concat(actualOption)
      }

      const option = {
        title: {
          text: title,
          textStyle: {
            color: 'black'
          }
        },
        grid: {
          left: 20,
          right: 50,
          bottom: 60,
          top: 60,
          containLabel: true
        },
        calculable: true,
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        xAxis: {
          type: 'category',
          data: xAxisData,
          boundaryGap: false
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            formatter: yAxisLabel || '{value}'
          },
          axisTick: {
            show: false
          }
        },
        legend: {
          y: 'bottom',
          padding: [20, 0, 20, 0],
          data: legend
        },
        series: seriesOption || []
      }

      this.chart.setOption(option)
    }
  }
}
</script>
